package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsPartBackDao;
import com.sugon.dao.ToolsPartOutDao;
import com.sugon.dao.ToolsStockDao;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.ToolsPartBackEntity;
import com.sugon.entity.ToolsPartOutEntity;
import com.sugon.entity.ToolsStockEntity;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsPartBackService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 退库记录Service实现类
 *
 * @author sugon
 * @date 2019-08-02 17:51:58
 */
@Service("toolsPartBackService")
public class ToolsPartBackServiceImpl implements ToolsPartBackService {
    @Autowired
    private ToolsPartBackDao toolsPartBackDao;
    @Autowired
    private ToolsPartOutDao toolsPartOutDao;
    @Autowired
    private ToolsStockDao toolsStockDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    @Override
    public ToolsPartBackEntity queryObject(String id) {
        return toolsPartBackDao.queryObject(id);
    }

    @Override
    public List<ToolsPartBackEntity> queryList(Map<String, Object> map) {
        return toolsPartBackDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsPartBackDao.queryTotal(map);
    }

    @Override
    public int save(ToolsPartBackEntity toolsPartBack) {
        toolsPartBack.setId(RandomUtil.randomString(32));
        toolsPartBack.setCreateUser(getUserId());
        toolsPartBack.setCreateDate(new Date());
        toolsPartBack.setBackDate(new Date());

        //计算退库数量 出库数量和库存
        //出库数量
        ToolsPartOutEntity toolsPartOutEntity = toolsPartOutDao.queryByOutCode(toolsPartBack.getOutCode());
        //库存
        ToolsStockEntity toolsStockEntity = toolsStockDao.queryByCode(toolsPartOutEntity.getSystemCode());
        int num = 0;
        int num2 = 0;
        if (toolsPartOutEntity.getRelNum() != null && toolsPartOutEntity.getRelNum() > 0) {
            num = toolsPartOutEntity.getRelNum() - toolsPartBack.getBackNum();
        } else {
            num = toolsPartOutEntity.getOutNum() - toolsPartBack.getBackNum();
        }
        num2 = toolsStockEntity.getStockNum() + toolsPartBack.getBackNum();
        toolsStockEntity.setStockNum(num2);
        toolsStockDao.update(toolsStockEntity);
        toolsPartOutEntity.setRelNum(num);
        toolsPartOutDao.update(toolsPartOutEntity);
       /* //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(toolsPartBack.getId(), ProcessInfoEnum.SPA_TKSP, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(toolsPartBack.getId());
        sysMsgEntity.setDataUrl("../tools/toolspartback.html?backCode=" + toolsPartBack.getBackCode());
        sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
        sysMsgEntity.setUserId(ShiroUtils.getUserId());
        sysMsgEntity.setDept(ShiroUtils.getDeptId());
        sysMsgEntity.setTitle("工装"+ProcessInfoEnum.SPA_TKSP.getDesc());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_TKSP);
        sysMsgService.allMsgData(sysMsgEntity);*/
        return toolsPartBackDao.save(toolsPartBack);
    }

    @Override
    public int update(ToolsPartBackEntity toolsPartBack) {
        toolsPartBack.setUpdateUser(getUserId());
        toolsPartBack.setUpdateDate(new Date());
        return toolsPartBackDao.update(toolsPartBack);
    }

    @Override
    public int delete(String id) {
        return toolsPartBackDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return toolsPartBackDao.deleteBatch(ids);
    }

    @Override
    public int review(ToolsPartBackEntity toolsPartBack) {
        /**
         * 审核通过,修改出库表中实际出库数量
         * 1.如果实际出库字段已经有数据 则直接用实际出库计算 否则用出库数量计算
         * 2.明细库存表中更新库存数量
         */
        toolsPartBack.setApprovalTime(new Date());
        toolsPartBack.setApprovalUser(getUserId());

        if (toolsPartBack.getApprovalStstus().equals(Constant.IS_ADOPT)) {
            ToolsPartOutEntity toolsPartOutEntity = toolsPartOutDao.queryByOutCode(toolsPartBack.getOutCode());
            ToolsStockEntity toolsStockEntity = toolsStockDao.queryByCode(toolsPartOutEntity.getSystemCode());
            int num = 0;
            int num2 = 0;
            if (toolsPartOutEntity.getRelNum() != null && toolsPartOutEntity.getRelNum() > 0) {
                num = toolsPartOutEntity.getRelNum() - toolsPartBack.getBackNum();
            } else {
                num = toolsPartOutEntity.getOutNum() - toolsPartBack.getBackNum();
            }
            num2 = toolsStockEntity.getStockNum() + toolsPartBack.getBackNum();
            toolsStockEntity.setStockNum(num2);
            toolsStockDao.update(toolsStockEntity);
            toolsPartOutEntity.setRelNum(num);
            toolsPartOutDao.update(toolsPartOutEntity);
        }
        sysProcessInfoService.saveProcessInfo(toolsPartBack.getId(), ProcessInfoEnum.SPA_TKSP, Integer.valueOf(toolsPartBack.getApprovalUser()), null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(toolsPartBack.getId());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_TKSP);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);
        toolsPartBackDao.update(toolsPartBack);
        return 0;
    }

    @Override
    public int statTotalNum(String posId, String month) {
        Integer totalNum = toolsPartBackDao.statTotalNum(posId, month);
        return totalNum == null ? 0 : totalNum;
    }

}
